package ssm.dao.impl;

import ssm.dao.Search;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class FacadeSearch implements Search {
    /**
     * 外观模式
     * @param conn
     * @param rs
     * @param rowsNumber
     * @throws SQLException
     */
    @Override
    public void check(Connection conn, ResultSet rs, int rowsNumber) throws SQLException {
        int count=0;
        String tempd1 = "";
        String tempd2 = "";
        String tempd3 = "";
        boolean flag = true;
        for (int i = 1; i <= rowsNumber; i++) { //1st for
            rs.absolute(i);
            String rel_type = rs.getString("RelationType");
            if (rel_type.equalsIgnoreCase("aggregation")) { //1st if

                String s1 = rs.getString("SourceClass");
                String d1 = rs.getString("DestinationClass");

                for (int j = 1; j <= rowsNumber; j++) { //2nd for
                    rs.absolute(j);
                    String rel_type2 = rs.getString("RelationType");
                    if (rel_type2.equalsIgnoreCase("aggregation")) { //2nd if
                        String s2 = rs.getString("SourceClass");
                        String d2 = rs.getString("DestinationClass");
                        if (!(d2.equalsIgnoreCase(d1)) && s1.equalsIgnoreCase(s2)) {// 3rd if not with self

                            for (int k = 1; k <= rowsNumber; k++) { //3rd for
                                rs.absolute(k);
                                String rel_type3 = rs.getString("RelationType");
                                if (rel_type3.equalsIgnoreCase("aggregation")) { //4th if
                                    String s3 = rs.getString("SourceClass");
                                    String d3 = rs.getString("DestinationClass");
                                    if (!(d3.equalsIgnoreCase(d1)) && !(d3.equalsIgnoreCase(d2)) && !(d2.equalsIgnoreCase(d1))) {//5th if
                                        if (s1.equalsIgnoreCase(s2) && s1.equalsIgnoreCase(s3)) {//6th if

                                            if (d1.equalsIgnoreCase(tempd1) || d1.equalsIgnoreCase(tempd2) || d2.equalsIgnoreCase(tempd3) || d2.equalsIgnoreCase(tempd1) || d2.equalsIgnoreCase(tempd2) || d2.equalsIgnoreCase(tempd3) || d3.equalsIgnoreCase(tempd3) || d3.equalsIgnoreCase(tempd3) || d3.equalsIgnoreCase(tempd3)) {
                                                flag = false;
                                            } // to prevent insert same values for d1 d2 d3
                                            if (flag) {

                                                String sql2 = "INSERT INTO designs.facade_instances(Facade,Subsystem1,Subsystem2,Subsystem3 ) VALUES (?,?,?,?)";
                                                PreparedStatement stmt2 = conn.prepareStatement(sql2);

                                                stmt2.setString(1, s1);
                                                stmt2.setString(2, d1);
                                                stmt2.setString(3, d2);
                                                stmt2.setString(4, d3);

                                                int rows2 = stmt2.executeUpdate();
                                                count+=rows2;
                                                tempd1 = d1;
                                                tempd2 = d2;
                                                tempd3 = d3;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        System.out.println("found " + count + " Facade");
    }
}
